package com.wngbms.aop._07;

import com.wngbms.aop._01.EchoServiceMethodInterceptor;
import com.wngbms.aop._06.EchoServicePointcut;
import com.wngbms.aop.staticproxy.DefaultEchoService;
import com.wngbms.aop.staticproxy.EchoService;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;

public class PointcutApiDemo {

    public static void main(String[] args) {
        EchoServiceEchoMethodPointcut pointcut = EchoServiceEchoMethodPointcut.INSTANCE;

        EchoServicePointcut echoServicePointcut = new EchoServicePointcut("echo", EchoService.class);
        ComposablePointcut pc = new ComposablePointcut(pointcut);
        pc.intersection(echoServicePointcut.getClassFilter());
        pc.intersection(echoServicePointcut.getMethodMatcher());

        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, new EchoServiceMethodInterceptor());

        DefaultEchoService defaultEchoService = new DefaultEchoService();
        ProxyFactory proxyFactory = new ProxyFactory(defaultEchoService);
        proxyFactory.addAdvisor(advisor);

        EchoService echoService = (EchoService) proxyFactory.getProxy();
        System.out.println(echoService.echo("tim"));

    }

}
